<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class obat_rusak extends CI_Controller {

	public function __construct(){
		parent::__construct();
		$this->load->model('m_obat_rusak');
		$this->load->library('breadcrumb');	
		$this->load->helper('id_date');
	}

	public function index(){
		$this->rule->type('R');
		$this->load->library('paging'); 

		$config['sql'] = $this->m_obat_rusak->get_all();
		$config['per_page'] = 6;
		$data = $this->paging->create_pagination($config);
		$judul = "Obat Rusak/Hilang";
		$data['judul'] = $judul;
		$this->layout->set_title($judul);
		$this->layout->set_desc("lorem ipsum");

		#breadcrumbs
		$this->breadcrumb->clear();
		$this->breadcrumb->add_crumb('Dasbor', site_url('dasbor'));
		$this->breadcrumb->add_crumb('Obat', site_url('obat_rusak#'));
		$this->breadcrumb->add_crumb($judul);

		$this->layout->set_include('js', 'themes/general/bundle/jquery_validation/jquery.validate.min.js', 'header');
		$this->layout->set_include('js', 'themes/general/bundle/jquery_validation/jquery.validate.min.js', 'header');
		$this->layout->set_include('js', 'themes/general/bundle/jquery_validation/localization/messages_id.min.js', 'header');
		$this->layout->set_include('js', 'themes/general/bundle/select2/select2.min.js', 'header');
		$this->layout->set_include('css', 'themes/general/bundle/select2/select2.min.css');
		
		$this->layout->set_include('js', 'themes/apotek/js/jquery-ui.js', 'header');
		$this->layout->set_include('js', 'themes/apotek/js/datepicker-id.js', 'header');
		$this->layout->set_include('css', 'themes/apotek/css/olide-datepicker.css');
		$this->layout->set_include('css', 'themes/apotek/css/olide-custom.css');

		//untuk motong kalimat
		$this->load->helper('trim_text');
		$data['obat'] = $this->m_obat_rusak->get_kelompok();

		//untuk format tanggal
		$this->load->helper('id_date');

		$this->layout->theme('apotek', 'obat_rusak/index', $data);
	}
	
	public function search(){
		$this->rule->type('R');
		$input = $this->input->post();
		$this->session->set_userdata('filter', $input['filter']);
		if($input != ""){			
			if($input['filter'] == "by_kelompok"){
				$this->db->where('KdKelompok', $input['keyword']);
				$r_kelompok = $this->db->get('kelompok')->result_array();
				$this->session->set_userdata('obat_rusak', $r_kelompok[0]['NmKelompok']);
			}else if($input['filter'] == "by_name"){
				$this->session->set_userdata('obat_rusak', $input['keyword']);
			}else if($input['filter'] == "by_tanggal"){
				$this->session->set_userdata('obat_rusak1', $input['keyword'][1]);
				$this->session->set_userdata('obat_rusak2', $input['keyword'][0]);
			}

			#pagination
			$this->load->library('paging');
			$config['sql'] = $this->m_obat_rusak->search($input);
			$config['per_page'] = 5;
			$data = $this->paging->create_pagination($config);

			if($input['filter'] != "by_tanggal"){
				$judul = 'Pencarian Obat Rusak/Hilang Dengan Kata Kunci "' . $this->session->userdata('obat_rusak') . '"';
				$data['judul']  = 'Pencarian Obat Rusak/Hilang Dengan Kata Kunci " <font color="#FC771F">' . $this->session->userdata('obat_rusak') . '</font> "';
			}else{
				if($input['keyword'][0] != "" && $input['keyword'][1] != ""){
					$judul = 'Pencarian Obat Rusak/Hilang Tanggal "' . $this->session->userdata('obat_rusak1') . '" s/d "'.$this->session->userdata('obat_rusak1').'"';
					$data['judul']  = 'Pencarian Obat Rusak/Hilang Tanggal "<font color="#FC771F">' . $this->session->userdata('obat_rusak1') . '</font>" <small>s/d</small> "<font color="#FC771F">'.$this->session->userdata('obat_rusak2').'</font>"';
				}else if($input['keyword'][0] == "" && $input['keyword'][1] == ""){
					$judul = 'Pencarian Obat Rusak/Hilang';
					$data['judul']  = 'Pencarian Obat Rusak/Hilang';		
				}else if($input['keyword'][0] == ""){
					$judul = 'Pencarian Obat Rusak/Hilang Sebelum Tanggal "' . $this->session->userdata('obat_rusak1') . '"';
					$data['judul']  = 'Pencarian Obat Rusak/Hilang Sebelum Tanggal "<font color="#FC771F">' . $this->session->userdata('obat_rusak1') . '</font>"';
				}else if($input['keyword'][1] == ""){
					$judul = 'Pencarian Obat Rusak/Hilang Setelah Tanggal "' . $this->session->userdata('obat_rusak2') . '"';
					$data['judul']  = 'Pencarian Obat Rusak/Hilang Setelah Tanggal "<font color="#FC771F">' . $this->session->userdata('obat_rusak2') . '</font>"';
				}
			}			

			$this->layout->set_title($judul);
			$this->layout->set_desc('Made with love by Raksa Abadi Informatika');

			$this->layout->set_include('js', 'themes/general/bundle/jquery_validation/jquery.validate.min.js', 'header');
			$this->layout->set_include('js', 'themes/general/bundle/jquery_validation/jquery.validate.min.js', 'header');
			$this->layout->set_include('js', 'themes/general/bundle/jquery_validation/localization/messages_id.min.js', 'header');
			$this->layout->set_include('js', 'themes/general/bundle/select2/select2.min.js', 'header');
			$this->layout->set_include('css', 'themes/general/bundle/select2/select2.min.css');

			$this->layout->set_include('js', 'themes/apotek/js/jquery-ui.js', 'header');
			$this->layout->set_include('js', 'themes/apotek/js/datepicker-id.js', 'header');
			$this->layout->set_include('css', 'themes/apotek/css/olide-datepicker.css');
			$this->layout->set_include('css', 'themes/apotek/css/olide-custom.css');

			#breadcrumbs
			$this->breadcrumb->clear();
			$this->breadcrumb->add_crumb('Dasbor', site_url('dasbor'));
			$this->breadcrumb->add_crumb('Obat', site_url('obat_rusak#'));
			$this->breadcrumb->add_crumb('Obat Rusak/Hilang', site_url('obat_rusak'));
			$this->breadcrumb->add_crumb($judul);

			$this->layout->set_include('css', 'themes/apotek/css/olide-custom.css');

			$this->load->helper('trim_text');
			
			//untuk format tanggal
			$this->load->helper('id_date');
			$data['obat'] = $this->m_obat_rusak->get_kelompok();

			$this->layout->theme('apotek','obat_rusak/index', $data);
		}else{
			redirect('obat_rusak', 'refresh');
		}
	}

	public function add(){
		$this->rule->type('C');

		if (isset($_POST) && !empty($_POST)) {
			$input  = $this->input->post();
			$this->m_obat_rusak->store($input);

			redirect('obat_rusak', 'refresh');
		}else{
			$judul = 'Tambah Obat Rusak/Hilang';
			$data['judul']  = $judul;

			$this->layout->set_title($judul);
			$this->layout->set_desc('Made with love by Raksa Abadi Informatika');
			#Jquery
			$this->layout->set_include('js', 'themes/general/bundle/jquery_validation/jquery.validate.min.js', 'header');
			$this->layout->set_include('js', 'themes/general/bundle/jquery_validation/localization/messages_id.min.js', 'header');
			$this->layout->set_include('js', 'themes/general/bundle/select2/select2.min.js', 'header');
			$this->layout->set_include('css', 'themes/general/bundle/select2/select2.min.css');

			$this->layout->set_include('js', 'themes/apotek/js/jquery-ui.js', 'header');
			$this->layout->set_include('js', 'themes/apotek/js/datepicker-id.js', 'header');
			$this->layout->set_include('css', 'themes/apotek/css/olide-datepicker.css');
			$this->layout->set_include('css', 'themes/apotek/css/olide-custom.css');

	    	#breadcrumbs
			$this->breadcrumb->clear();
			$this->breadcrumb->add_crumb('Dasbor', site_url('dasbor'));
			$this->breadcrumb->add_crumb('Obat', site_url('obat_rusak#'));
			$this->breadcrumb->add_crumb('Obat Rusak/Hilang', site_url('obat_rusak'));
			$this->breadcrumb->add_crumb($judul);

			$this->layout->theme('apotek','obat_rusak/add', $data);
		}
	}

	public function edit($id){
		$this->rule->type('U');
		if (isset($_POST) && !empty($_POST)) {
			$input  = $this->input->post();
			$this->m_obat_rusak->update($input);

			redirect('obat_rusak', 'refresh');
		}else{
			$this->load->helper('cek_id');
			if(!cek_id($id, 'KdObatHilang', 'obat_hilang')){
				show_404();
			}

			$this->layout->set_include('js', 'themes/general/bundle/jquery_validation/jquery.validate.min.js', 'header');
			$this->layout->set_include('js', 'themes/general/bundle/jquery_validation/localization/messages_id.min.js', 'header');
			$this->layout->set_include('js', 'themes/general/bundle/select2/select2.min.js', 'header');
			$this->layout->set_include('css', 'themes/general/bundle/select2/select2.min.css');

			$this->layout->set_include('js', 'themes/apotek/js/jquery-ui.js', 'header');
			$this->layout->set_include('js', 'themes/apotek/js/datepicker-id.js', 'header');
			$this->layout->set_include('css', 'themes/apotek/css/olide-datepicker.css');
			$this->layout->set_include('css', 'themes/apotek/css/olide-custom.css');
			
			$judul = 'Edit Obat Rusak/Hilang';
			$data['judul'] = $judul;

			$this->layout->set_title($judul);
			$this->layout->set_desc('Made with love by Raksa Abadi Informatika');

  			#breadcrumbs
			$this->breadcrumb->clear();
			$this->breadcrumb->add_crumb('Dasbor', site_url('dasbor'));
			$this->breadcrumb->add_crumb('Obat', site_url('obat_rusak#'));
			$this->breadcrumb->add_crumb('Obat Rusak/Hilang', site_url('obat_rusak'));
			$this->breadcrumb->add_crumb($judul);

			$data['data'] = $this->m_obat_rusak->get_by_id($id);

			$this->layout->theme('apotek','obat_rusak/edit', $data);
		}
	}

	public function delete($id){
		$this->rule->type('D');

		$this->load->helper('cek_id');
		if(!cek_id($id, 'KdObatHilang', 'obat_hilang')){
			set_flash('danger','Ups, Data Obat Rusak/HIlang tidak ditemukan');
			redirect('obat_rusak','refresh');
		}

		$this->m_obat_rusak->destroy($id);

		redirect($_SERVER['HTTP_REFERER'],'refresh');
	}

	public function excel($filter="", $param1="", $param2=""){
		$this->rule->type('R');
		
		#load our new PHPExcel library
		$this->load->library('excel');
		#activate worksheet number 1
		$this->excel->setActiveSheetIndex(0);
		#name the worksheet
		$this->excel->getActiveSheet()->setTitle('Obat Rusak'.site_title());

		#set cell A1 content with some text
		if($filter != ""){
			if($filter == "by_name"){
				$this->excel->getActiveSheet()->setCellValue('A1', 'Obat Rusak Dengan Kata Kunci "'.$param1.'" '.site_title());
			}else if($filter == "by_kelompok"){
				$this->db->where('KdKelompok', $param1);
				$klpk = $this->db->get('kelompok')->result_array();
				$this->excel->getActiveSheet()->setCellValue('A1', 'Obat Rusak Dengan Kata Kunci "'.$klpk[0]['NmKelompok'].'" '.site_title());
			}else if($filter == "by_tanggal"){
				if($param1 != "null" && $param2 != "null"){
					$this->excel->getActiveSheet()->setCellValue('A1', 'Obat Rusak Tanggal "'.$param1.'" s/d "'.$param2.'" '.site_title());
				}else if($param1 == "null" && $param2 == "null"){
					$this->excel->getActiveSheet()->setCellValue('A1', 'Obat Rusak '.site_title());
				}else if($param1 == "null"){
					$this->excel->getActiveSheet()->setCellValue('A1', 'Obat Rusak Setelah Tanggal "'.$param2.'" '.site_title());		
				}else if($param2 == "null"){
					$this->excel->getActiveSheet()->setCellValue('A1', 'Obat Rusak Sebelum Tanggal "'.$param1.'" '.site_title());		
				}
			}
		}else{
			$this->excel->getActiveSheet()->setCellValue('A1', 'Obat Rusak '.site_title());	
		}
		
		$this->excel->getActiveSheet()->setCellValue('A2', 'No.');
		$this->excel->getActiveSheet()->setCellValue('B2', 'Kode Obat Rusak');
		$this->excel->getActiveSheet()->setCellValue('C2', 'Nama Obat');
		$this->excel->getActiveSheet()->setCellValue('D2', 'Tanggal');
		$this->excel->getActiveSheet()->setCellValue('E2', 'Jumlah');
		$this->excel->getActiveSheet()->setCellValue('F2', 'Keterangan');
		#change the font size
		$this->excel->getActiveSheet()->getStyle('A1')->getFont()->setSize(11);
		#make the font become bold
		$this->excel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
		#merge cell A1 until D1
		$this->excel->getActiveSheet()->mergeCells('A1:F1');
		#set aligment to center for that merged cell (A1 to D1)
		$this->excel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		#set style
		$TitleStyle = array(
			'borders' => array(
				'allborders' => array(
					'style' => PHPExcel_Style_Border::BORDER_THIN
					)
				),
			'fill' => array(
				'type' => PHPExcel_Style_Fill::FILL_SOLID,
           // 'color' => array('rgb' => '00994C') // hijau tua
				),
			'font'  => array(
				'bold'  => true,
  			//'color' => array('rgb' => '000000'),
				'size'  => 12,
				'name'  => 'calibri'
				)
			);

		$HeaderStyle = array(
			'borders' => array(
				'allborders' => array(
					'style' => PHPExcel_Style_Border::BORDER_THIN
					)
				),
			'fill' => array(
				'type' => PHPExcel_Style_Fill::FILL_SOLID,
            //'color' => array('rgb' => '32CD32') // hijau
				),
			'font'  => array(
				'bold'  => true,
				'size'  => 12,
				'name'  => 'calibri'
				)
			);

		$ContentStyle = array(
			'borders' => array(
				'allborders' => array(
					'style' => PHPExcel_Style_Border::BORDER_THIN
					)
				),
			'fill' => array(
				'type' => PHPExcel_Style_Fill::FILL_SOLID,
            //'color' => array('rgb' => 'FFFF00') // kuning
				),
			'font'  => array(
				'size'  => 12,
				'name'  => 'calibri'
				)
			);

		$this->excel->getActiveSheet()->getStyle('A1:F1')->applyFromArray($TitleStyle);
		$this->excel->getActiveSheet()->getStyle('A2:F2')->applyFromArray($HeaderStyle);
		//isi
		if($filter != ""){
			$list_data = $this->m_obat_rusak->export_by($filter, $param1, $param2);
		}else{
			$list_data = $this->m_obat_rusak->export();
		}

		$cols = 3;
		$num = 1;
		foreach($list_data as $row){
			$col = $cols++;
			$this->excel->getActiveSheet()->setCellValue('A' . $col, $num++);
			$this->excel->getActiveSheet()->setCellValue('B' . $col, $row['KdObatHilang']);
			$this->excel->getActiveSheet()->setCellValue('C' . $col, $row['NmObat']);
			$this->excel->getActiveSheet()->setCellValue('D' . $col, $row['Tanggal']);
			$this->excel->getActiveSheet()->setCellValue('E' . $col, $row['Jumlah']);
			$this->excel->getActiveSheet()->setCellValue('F' . $col, $row['Keterangan']);
		}

		#warna
		$cols2 = 3;
		foreach($list_data as $row){
			$col2 = $cols2++;
			$this->excel->getActiveSheet()->getStyle('A' . $col2)->applyFromArray($ContentStyle);
			$this->excel->getActiveSheet()->getStyle('B' . $col2)->applyFromArray($ContentStyle);
			$this->excel->getActiveSheet()->getStyle('C' . $col2)->applyFromArray($ContentStyle);
			$this->excel->getActiveSheet()->getStyle('D' . $col2)->applyFromArray($ContentStyle);
			$this->excel->getActiveSheet()->getStyle('E' . $col2)->applyFromArray($ContentStyle);
			$this->excel->getActiveSheet()->getStyle('F' . $col2)->applyFromArray($ContentStyle);
		}

		$this->excel->getActiveSheet()->getColumnDimension("A")->setAutoSize(true);
		$this->excel->getActiveSheet()->getColumnDimension("B")->setAutoSize(true);
		$this->excel->getActiveSheet()->getColumnDimension("C")->setAutoSize(true);
		$this->excel->getActiveSheet()->getColumnDimension("D")->setAutoSize(true);
		$this->excel->getActiveSheet()->getColumnDimension("E")->setAutoSize(true);
		$this->excel->getActiveSheet()->getColumnDimension("F")->setAutoSize(true);

		$filename='data obat rusak.xls'; //save our workbook as this file name
		header('Content-Type: application/vnd.ms-excel'); //mime type
		header('Content-Disposition: attachment;filename="'.$filename.'"'); //tell browser what's the file name
		header('Cache-Control: max-age=0'); //no cache

		//save it to Excel5 format (excel 2003 .XLS file), change this to 'Excel2007' (and adjust the filename extension, also the header mime type)
		//if you want to save it as .XLSX Excel 2007 format
		$objWriter = PHPExcel_IOFactory::createWriter($this->excel, 'Excel5');  
		//force user to download the Excel file without writing it to server's HD
		$objWriter->save('php://output');
	}

}

/* End of file obat_rusak.php */
/* Location: ./application/controllers/obat_rusak.php */